/*认购*/
define(['jquery','api','user','util'],function($,api,user,util){
	var vm,
	   time,
	   startid,
	   // 应价类型:'1' - 挂单;'2' - 应价;'3' - 认购;'4' - 抢购;'5' - 大额配售;'6' - 划拨;
	   TYPES={'49':'挂单','50':'应价','51':'认购','52':'抢购','53':'大额配售','54':'划拨'},
	   controller = {
	   init:function(id){
	   		var that=this;
	   	   if(!user.checkLogin())return;
	   	   
	   	   	api.headerShow(1);
			api.headerTitle(L('html_title_order_transactionrecord'));
		   api.setTitle(L('html_title_order_transactionrecord'));	
		   
		   	this.render();
	   },
	   render:function(){
	   	  var that=this;
	   	  	$('#common').html('<div id="orderTransactionRecord"></div>');

	       require(['text!pages/order/transactionrecord.html','Vue'],function(tpl,Vue){
	            vm=new Vue({
	               el:'#orderTransactionRecord',
	               data:{
	               	  show:true,
	               	  ordersHistory:[], // 历史列表数据容器
	               	  ordersToday:[], // 今天列表数据容器
	               	  loading:false,
                        startTime:(function(d){return d.format('yyyy-MM-dd')})(new Date(new Date().getTime()-86400*1000)),
                        endTime:(function(d){return d.format('yyyy-MM-dd')})(new Date()),
                        form:{
	                    	startTime:'',
	                    	endTime:''
	                    },
                        isToday:true, // tab 是否为今天，否则历史
                        showMore:{ // 显示更多
                              today:false,
                              history:false
                        },
                        noMoreNow:false,
                        noMoreHis:false
	               },
	               filters:{
	                 lowHigh:util.lowHigh, //方向
                        //金额格式化
                        priceFormat:function(v,mindecimal){
                        	if(isNaN(v)){return v=''}; // 20161209dyx
                            return util.priceFormat(mindecimal,v);
                        },
                        typeName:function(o){
                        	var direct='',
                        		v = o.anspricetype;
                    		if(TYPES[v]==undefined){
                    			return v;
                    		}
                        	if((v==49 || v==50) && o.direction==98){//买入
                        		direct='买入';
                        	}
                        	else if((v==49 || v==50) && o.direction==115){//卖出
                        		direct='卖出';
                        	}
                        	return TYPES[v]+direct ? TYPES[v]+direct : '';
                        }
	              },
	               methods:{
				 	 query:function(more){
		                if(more === undefined){
			                this.ordersHistory=[];
		                    startid=0;
		                }
		                
		                if(this.form.startTime==''){
			            	this.form.startTime = this.startTime;
			            }
			            if(this.form.endTime==''){
							this.form.endTime = this.endTime;            	
			            }
		                that.load();
				 	 },
				 	 switchTab:function(t){
		              	 this.isToday=t;
		             }
	               },
	               template:tpl
	            });
	            
		       that.loadNow();
	       });
	       
	   },
	   load:function(){ //查历史
			vm.loading = true;
            var orders,
            	pageSize=20,
            	//50 降序
            	params={startday:vm.form.startTime,endday:vm.form.endTime,id:startid,count:pageSize,sort:50};
            	
	   	   	api.tradesOrderHis(params,
		   	   	function(rs){
	                vm.loading = false;
	               if(rs.retcode=='0' && rs.data){
	               		vm.noMoreHis=false;
	                    orders=rs.data;
	                    vm.ordersHistory=vm.ordersHistory.length?vm.ordersHistory.concat(orders):orders;
	                    var len = orders.length;
	                    startid+=len;
	                    if(len>=pageSize){
	                       vm.showMore.history=true;
	                    }else{
	                       vm.showMore.history=false;
	                    }
	               }else{
	               		vm.noMoreHis=true;
	//             		util.msg('暂无更多数据');
	               }
	   	   		},
	   	   		function(){//err
	   	   			vm.loading = false;
	   	   			vm.noMoreHis=false;
	   	   		}
   	   		);
	   },
	   loadNow:function(){ //当日
	   	  var params={};
	   	  vm.loading = true;
	      api.tradesOrderNow(params,
		      	function(rs){
		         vm.loading = false;
		         if(rs.retcode=='0' && rs.data){
		         	vm.noMoreNow=false;
			         vm.ordersToday=rs.data;
		      	}else{
		      		vm.noMoreNow=true;
	//	      		util.msg('暂无更多今日数据');
		      	}
		      },
		      function(){//err
		      	vm.loading = false;
		      	vm.noMoreNow=false;
		      }
      	);
	      
	   },
	   onRouteChange:function(){
	       if(vm)vm.show=false;
	   }
	};
    return controller;
})